<?php

namespace JinShaYinSha\Controller;

define ( 'ErrCode_Normal', 0000 );
define ( 'ErrCode_Distance', 0001 );
define ( 'ErrCode_Maintain', 0002 );

define ( 'ErrCode_Dialog', 3000 );
define ( 'ErrCode_Toast', 3001 );
define ( 'ErrCode_Sign', 3002 );
define ( 'ErrCode_Other', 3003 );

define ( 'ErrMsg_Normal', '正常' );
define ( 'ErrMsg_Distance', '该账号已下线或异地登录' );
define ( 'ErrMsg_Maintain', '系统维护中' );

define ( 'ErrMsg_Dialog', '弹窗错误' );
define ( 'ErrMsg_Toast', 'Toast错误' );
define ( 'ErrMsg_Sign', '校验数据错误' );
define ( 'ErrMsg_Other', '其他错误' );

define ( 'ErrMsg_Sys', '系统繁忙，请稍后再试！' );
use Think\Controller;

class BaseController extends Controller {
	/**
	 * 消息进
	 *
	 * @return boolean|string
	 */
	protected function mb_OnMessage() {
		if (I ( 'get.data' ) != null) {
			$tData = str_replace ( '&quot;', '"', I ( 'get.data' ) );
		} else if (I ( 'post.data' ) != null) {
			$tData = str_replace ( '&quot;', '"', I ( 'post.data' ) );
		} else {
			return false;
		}
		$reqData = json_decode($tData);
		$data = $reqData->m_Data;
		$sign = $reqData->m_Sign;
		
		if ($sign == md5 ( $data . C ( 'MSG_MD5' ) )) {
			return json_decode ( $data );
		} else {
			\Tools::mb_LogResult ( "--------SignError--------" );
			\Tools::mb_LogResult ( $tData );
			\Tools::mb_LogResult ( 'Data:' . json_encode ( $pwData ) );
			\Tools::mb_LogResult ( 'Sign:' . $sign );
			\Tools::mb_LogResult ( 'Server:' . md5 ( json_encode ( $pwData ) . C ( 'MSG_MD5' ) ) );
			\Tools::mb_LogResult ( "-------------------------" );
			
			$this->mb_OutMessage ( ErrCode_Sign, ErrMsg_Sign );
			
			exit ();
		}
	}
	/**
	 * 消息出
	 *
	 * @param unknown $result        	
	 * @param unknown $data        	
	 */
	protected function mb_OutMessage($result, $data) {
		$repData = new \stdClass ();
		$repData->m_Result = $result;
		$repData->m_Data = $data;
		$repData->m_Sign = md5 ( $result . $data . C ( 'MSG_MD5' ) );
		echo json_encode ( $repData );
	}
	/**
	 * 普通日志
	 *
	 * @param string $content
	 *        	内容
	 */
	protected function mb_NorLog($content) {
		\Tools::mb_LogResult ( $content );
	}
	/**
	 * 成功日志
	 *
	 * @param string $command
	 *        	命令
	 * @param string $content
	 *        	内容
	 */
	protected function mb_SuccLog($controller, $command, $content) {
		$path = "Succ/$controller";
		\Tools::mb_LogResult ( "--------$command-------", $path );
		\Tools::mb_LogResult ( "输出：" . $content, $path );
		\Tools::mb_LogResult ( "-----------------------", $path );
	}
	/**
	 * 错误日志
	 *
	 * @param string $command
	 *        	命令
	 * @param string $errCode
	 *        	错误代码
	 * @param string $errMsg
	 *        	错误信息
	 */
	protected function mb_ErrLog($controller, $command, $errCode, $errMsg, $param = true) {
		$path = "Err/$controller";
		\Tools::mb_LogResult ( "--------$command-------", $path );
		if ($param) {
			$tData = '';
			if (I ( 'get.data' ) != null) {
				$tData = str_replace ( '&quot;', '"', I ( 'get.data' ) );
			} else if (I ( 'post.data' ) != null) {
				$tData = str_replace ( '&quot;', '"', I ( 'post.data' ) );
			}
			\Tools::mb_LogResult ( "参数：" . $tData, $path );
		}
		\Tools::mb_LogResult ( "错误码：" . $errCode, $path );
		\Tools::mb_LogResult ( "错误信息：" . $errMsg, $path );
		\Tools::mb_LogResult ( "-----------------------", $path );
	}
	/**
	 * 输出一个日志数组
	 *
	 * @param string[] $log        	
	 */
	protected function mb_NorLogArray($log) {
		foreach ( $log as $key => $value ) {
			if (is_string ( $value )) {
				\Tools::mb_LogResult ( "$key => $value" );
			} else if (is_array ( $value )) {
				\Tools::mb_LogResult ( "-------------$key-------------" );
				$this->mb_LogArray ( $value );
				\Tools::mb_LogResult ( "------------------------------" );
			} else {
			}
		}
	}
	protected function mb_CheckAdminIsLoggedIn($token) {
		$adminInfo = M ( 'Admininfo' );
		$adminData = $adminInfo->where ( "token='$token'" )->find ();
		if (isset ( $adminData ) == false) {
			$this->mb_OutMessage ( ErrCode_Distance, ErrMsg_Distance );
			exit ();
		}
		$ret = new \stdClass ();
		$ret->info = $adminInfo;
		$ret->data = $adminData;
		return $ret;
	}
	
	// /**
	// * 检查用户是否登录中
	// */
	// protected function mb_CheckAdminIsLoggedIn($token) {
	// $gmAdmin = new GmAdminModel ();
	// $admin = $gmAdmin->mb_GetAdminForToken ( $token );
	// if ($admin === false) {
	// $this->mb_OutMessage ( ErrCode_Dialog, ErrMsg_Sys );
	// exit ();
	// }
	// if (isset ( $admin ) == false) {
	// $this->mb_OutMessage ( ErrCode_Distance, ErrMsg_Distance );
	// exit ();
	// }
	// return $admin;
	// }
}